package com.bishe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.entity.Association;
import com.bishe.entity.Clue;
import com.bishe.entity.PersonnelInfo;
import com.bishe.entity.dto.PersonnelInfoDto;
import com.bishe.entity.query.ClueQuery;
import com.bishe.mapper.ClueMapper;
import com.bishe.service.AssociationService;
import com.bishe.service.ClueService;
import com.bishe.service.PersonnelInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author wwz
* @description 针对表【clue】的数据库操作Service实现
* @createDate 2024-03-04 13:57:33
*/
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue>
    implements ClueService {

    @Resource
    private PersonnelInfoService personnelInfoService;

    @Resource
    private AssociationService associationService;

    @Override
    public IPage<Clue> getPage(ClueQuery query) {
        return page(query.toPage(), Wrappers.lambdaQuery(Clue.class)
                .like(StrUtil.isNotBlank(query.getNature()),Clue::getNature,query.getNature())
                .like(StrUtil.isNotBlank(query.getProcessingProgress()),Clue::getProcessingProgress,query.getProcessingProgress())
                .like(StrUtil.isNotBlank(query.getSources()),Clue::getSources,query.getSources())
        );
    }

    @Override
    public List<?> selectPersonnelInfo(String id) {

        List<Association> associationList = associationService.list(Wrappers.lambdaQuery(Association.class).eq(Association::getCid, id));
        if (CollUtil.isEmpty(associationList)){
            return Collections.emptyList();
        }

        Map<String, Boolean> map = associationList.stream().collect(Collectors.toMap(Association::getPid, Association::getType));

        List<String> pids = associationList.stream().map(Association::getPid).collect(Collectors.toList());
        List<PersonnelInfo> personnelInfoList = personnelInfoService.list(Wrappers.lambdaQuery(PersonnelInfo.class).in(PersonnelInfo::getId, pids));

        List<PersonnelInfoDto> personnelInfoDtos = BeanUtil.copyToList(personnelInfoList, PersonnelInfoDto.class);
        for (PersonnelInfoDto personnelInfoDto : personnelInfoDtos) {
            Boolean type = map.get(personnelInfoDto.getId());
            personnelInfoDto.setPersonnelType(type);
        }
        return personnelInfoDtos;
    }
}




